#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
#from django.contrib import auth
from django.contrib.auth.models import User

from models import Profile
from forms import ProfileForm, UserForm

# Create your views here.
def register(request):
    """
    new user register.
    
    """
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            username = user_form.cleaned_data.get('login_name')
            password = user_form.cleaned_data['password']
            email = user_form.cleaned_data['email']
            new_user = User.objects.create_user(username=username, email=email, password=password)
            new_user.is_active = False
            if new_user.save():
                # save new profile for user.
                new_profile = profile_form.save(commit=False)
                new_profile.user = new_user
                new_profile.save()
            # if success
            return HttpResponseRedirect('/')
    else:
        user_form = UserForm()
        profile_form = ProfileForm()
    return render_to_response('registration/register.html', 
            {'user_form': user_form, 'profile_form': profile_form},
            context_instance=RequestContext(request))

